var date=echartUtil.GetDateStr(0);
var deptName="";
var sort=0;
var table;
//相关业务逻辑
$(function(){
	page.bind();
	nameToIdUtil.getProductionDepartment("productionDepartment");
	page.getDutyDate(deptName,date,null);
	echartUtil.getManager(deptName);
	$("#yestarday").val(date);
});


//时间改变
function dataChange(){
	date=$("#yestarday").val();
//	page.getDutyDate(deptName,date,null);
	table.ajax.reload();
	page.getQualified(deptName);
	page.getDuty(deptName,date);
}

var page={//页面总变量
		bind:function(){
			$("#sort").bind("click",function(){
				if(sort==0){
					sort=1;
				}else if(sort==1){
					sort=2
				}else{
					sort=1;
				}
				page.getDutyDate(deptName,date,sort);
			});
			
			$("#totalTable").bind("change",function(){
				deptName=$(this).val();
				page.getDutyDate(deptName,date,null);
				echartUtil.getManager(deptName);
			});
			
			$("#downlodeExcle").bind("click",function(){
				var url="workshop/downlodeMantissa?workShop="+deptName+"&date="+date+"&sort="+sort;
				tableUtil.download(url);
			});
			
			$("#isHidden").bind("click",function(){
				var text=$(this).text();
				if(text=="隐藏"){
					$(".middle").hide();
					$(this).text("显示");
				}else{
					$(".middle").show();
					$(this).text("隐藏");
				}
			});
			
			$("#productionDepartment").bind("change",function(){
				var deptId=$("#productionDepartment").val();
				page.getQualified(deptId);
			});
		},
		getDutyDate:function(deptName,date,sort){
			table=ajaxUtil.dataTablesAjaxNotFxiaction("dutyTable",10,"workshop/getDutyDate",
					{'workShop':deptName,'date':date,'sort':sort},[
					{"data":"orderId",render: function (data,type,row) {
			            return "<a href='orderDetail.jsp?orderId="+data+"'>"+row.companyOrderId+"</a>";
			          }},
					{"data":"customerId"},
					{"data":"factoryId"},
					{"data":"color"},
					{"data":"orderCount"},
					{"data":"feddingCount"},
					{"data":"inCount"},
					{"data":"outCount"},
					{"data":"shortCount"},
					{"data":"qualifiedCount"},
					{"data":"lossRate",render: function (data,type,row) {
						var lossRate=data;
						lossRate=(lossRate*100).toFixed(2);
						return lossRate+'%';
					}},
					{"data":"noQualifiedCount"},
					{"data":"lackCount",render: function (data,type,row) {
						if(data<=0){
							data=0;
						}
						if(data>0){
							return "<span style='color:red'>"+data+"</span>";
						}else{
							return data;
						}
					}},
					{"data":"workShopName"},
					{"data":"procedur"},
					{"data":"userName"},
					{"data":"noQuealtyReasonList",render: function (data,type,row) {
						var str="";
						if(row.noQualifiedCount >0){
							for(var i=0;i<data.length;i++){
								str=str+" "+data[i];
							}
						}
			            return str;
			          }}
			]);
			page.getDuty(deptName,date);
//			
//			ajaxUtil.ajaxRuquest('workshop/getDutyDate',{'workShop':workShop,'date':date},function(data){
//				if(data.length>0){
//					   var html="<tbody>";
//					   for(var key in data){
//						   html+="<tr><td><a class='order'>"+data[key].orderId
//					       +"</a></td><td>"+data[key].customerId
//					       +"</td><td>"+data[key].factoryId
//					       +"</td><td>"+data[key].color
//					       +"</td><td>"+data[key].orderCount
//					       +"</td><td>"+data[key].feddingCount
//					       +"</td><td>"+data[key].qualifiedCount
//					       +"</td><td>"+data[key].noQualifiedCount
//					       +"</td><td>"+data[key].lackCount
//					       +"</td><td>"+data[key].procedur
//					       +"</td><td>"+data[key].userName
//					       +"</tr>";
//					   }
//					   html+="</tbody>";
//					   $("#dutyTable tbody").html("");
//					   $("#dutyTable").append(html);
//					   aBindUtil.aOrderBind();
//				   }else{
//					   $("#dutyTable tbody").html("");
//					   layer.msg('该车间没有数据', {icon: 1});
//				   }
//			});
		},
		//车间合格率看板
		getQualified:function(deptId){
			nameToIdUtil.getWorkShopNameAndId("totalTable",deptId);
//			ajaxUtil.ajaxRuquest("workshop/getWorkshopName",null,function(data){
//				var html="";
//				for(var i=0;i<data.length;i++){
//					html+="<option value='"+data[i]+"'>"+data[i]+"</option>";
//				}
//				$("#totalTable").append(html);
//			});
			
			
			ajaxUtil.ajaxRuquestNoQuan('workshop/getQualified',{'date':date,"deptId":deptId},function(data){
				var workShopArray=new Array();
				var workDataArray=new Array();
				for(var i=0;i<data.length;i++){
					workShopArray.push(data[i].deptName);
					workDataArray.push(data[i].reach);
				}
				echartUtil.barChartTemp('qualifiedDiv','车间合格率看板',['合格率'],workShopArray,workDataArray,'%');
			});
		},
		//拖期看板
		getDuty:function(workShop,date){
			if(workShop==''){
				$("#dutyDiv").html("");
				return false;
			}
			ajaxUtil.ajaxRuquestNoQuan('workshop/getDuty',{'workShop':workShop,'date':date},function(data){
				var userArray=new Array();
				var userDataArray=new Array();
				for(var key in data){
					userArray.push(data[key].deptName);
					userDataArray.push(data[key].reach);
				}
				echartUtil.barChartTemp('dutyDiv','尾数造成责任看板',["不良品数"],userArray,userDataArray,'');
			});
		}
}

setInterval ("dataChange()", 900000);//十五分钟更新一次数据
